import { request } from '../../utils/request';
Page({
  data: {
    id:'',
    orderStatus:'',
    reason:'',
    reasons: [
      { text: '不需要此项服务了', isSelected: false },
      { text: '费用有点贵', isSelected: false },
      { text: '临时有事，不方便服务', isSelected: false },
      { text: '信息填写错误', isSelected: false },
      { text: '重复下单', isSelected: false }
    ],
    hasSelected: false // 标记是否有选中项，控制按钮颜色
  },

  // 单选+重复点击取消逻辑，同时更新reason字段
  selectReason(e) {
    const index = e.currentTarget.dataset.index;
    const reasons = [...this.data.reasons];
    const currentItem = reasons[index];
    
    // 处理选中状态
    if (currentItem.isSelected) {
      currentItem.isSelected = false; // 取消选中
    } else {
      reasons.forEach(item => (item.isSelected = false)); // 取消其他项选中
      currentItem.isSelected = true; // 选中当前项
    }
    
    // 更新选中状态和reason字段
    const hasSelected = reasons.some(item => item.isSelected);
    let reasonText = '';
    if (hasSelected) {
      reasonText = reasons.find(item => item.isSelected).text;
    }
    this.setData({ 
      reasons, 
      hasSelected, 
      reason: reasonText 
    });
  },

  // 提交按钮点击事件
  async submit() {
    if (this.data.hasSelected) {
      const { id, orderStatus, reason } = this.data;
      const requestData = {
        id,
        orderStatus,
        cancelReason:reason
      };
      console.log(requestData)
      const res = await request("/greenold-order/order",requestData,'PUT')
      wx.showToast({ title: '提交成功', icon: 'success' });
      // 1s后跳转
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/order-list/order-list', // 目标页面路径
        });
        // （可选）跳转后隐藏提示
        this.setData({ showToast: false });
      }, 1000); // 2000 毫秒 = 2 秒
    } else {
      wx.showToast({ title: '请选择原因', icon: 'none' });
    }
  },

  onLoad(options) {
    const id = options.id; 
    const orderStatus = options.orderStatus; 
    this.setData({
      id:id,
      orderStatus:orderStatus
    })
  }

});